package com.drought.rained.auth;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.BindedCardDataLJ;
import com.drought.rained.bean.DictionaryItemDataLJ;
import com.drought.rained.business.AuthRouterLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.BankCardMvpViewLJ;
import com.drought.rained.mvpview.DictionaryMvpViewLJ;
import com.drought.rained.presenter.BankCardPresenterLJ;
import com.drought.rained.presenter.DictionaryPresenterLJ;
import com.drought.rained.response.AuthInfoResponseLJ;
import com.drought.rained.response.BindedCardResponseLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.response.PersonalInfoResponseLJ;
import com.drought.rained.utils.MaskUtilsLJ;
import com.drought.rained.utils.StringUtilsLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.drought.rained.view.MenuDialogLJ;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "银行卡认证", path = RouterMapLJ.BANK_AUTH_PAGE)
public class BankAuthActivityLJ extends BaseTitleActivity implements BankCardMvpViewLJ, DictionaryMvpViewLJ {

    @BindView(R.id.edit_real_name)
    EditText editRealName;
    @BindView(R.id.edit_bank_number)
    EditText editBankNumber;
    @BindView(R.id.edit_select_bank)
    EditText editSelectBank;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;
    @Autowired(name = "is_change")
    public boolean is_change;
    private BankCardPresenterLJ presenter;
    private DictionaryPresenterLJ dictionaryPresenter;
    private ArrayList<DictionaryItemDataLJ> mBankList;
    private final String DICTIONARY_TYPE_BANK_LIST = "common_bankname";
    private MenuDialogLJ mMenuDialog;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        presenter = new BankCardPresenterLJ(this);
        dictionaryPresenter = new DictionaryPresenterLJ(this);
        List<String> types = new ArrayList<>();
        types.add(DICTIONARY_TYPE_BANK_LIST);
        dictionaryPresenter.getDictionaryData(types);
        presenter.getPersonInfo();
        if (UserInfoLJ.isShowUserInformation())
            presenter.getBankInfo();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_bank_auth_lj;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.bank_card_page_name);
    }

    @OnClick({R.id.tv_next_step, R.id.edit_select_bank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next_step:
                submit();
                break;
            case R.id.edit_select_bank:
                pickerBank();
                break;
        }
    }

    private void submit() {
        String checkParameter = checkParameter();
        if (!StringUtilsLJ.isEmpty(checkParameter)) {
            ToastUtilsLJ.showShort(this, checkParameter);
            return;
        }
        tvNextStep.setClickable(false);
        String realName = editRealName.getText().toString().trim();
        String bankCode = editSelectBank.getTag().toString();
        String bankCard = editBankNumber.getText().toString().trim();
        String bankName = editSelectBank.getText().toString().trim();
        presenter.saveBankCard(realName, bankCode, bankName, bankCard);
        showLoadingView();
    }

    private String checkParameter() {
        String str = "";
        if (StringUtilsLJ.isEmpty(editSelectBank.getText().toString().trim()) || editSelectBank.getTag() == null) {
            str = getStringResource(R.string.bank_card_type_bank_number_hint) + getStringResource(R.string.bank_card_bank_of_deposit_hint);
        }
        if (StringUtilsLJ.isEmpty(editBankNumber.getText().toString().trim())) {
            str = getStringResource(R.string.bank_card_type_bank_number_hint) + getStringResource(R.string.bank_card_bank_number_hint);
        }
        if (StringUtilsLJ.isEmpty(editRealName.getText().toString().trim())) {
            str = getStringResource(R.string.bank_card_type_bank_number_hint) + getStringResource(R.string.bank_card_name_hint);
        }
        return str;
    }

    @Override
    public void onGetDataNext(BindedCardResponseLJ response) {
        dismissLoadingView();
        fillTextView(response.getData());
    }

    @Override
    public void onGetDataNext(BlankResponseLJ response) {

        dismissLoadingView();
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            if (!is_change) {
                AuthRouterLJ.getInstance().jumpNextPager(this, RouterMapLJ.BANK_AUTH_PAGE);
            } else {
                finish();
            }

        } else {
            ToastUtilsLJ.showShort(this, response.getMessage());
        }
        tvNextStep.setClickable(true);

    }

    @Override
    public void onGetDataNext(AuthInfoResponseLJ response) {}

    @Override
    public void onGetDataNext(PersonalInfoResponseLJ response) {
        if(response.getStatus()== ConstantLJ.HTTP_OK){
            MaskUtilsLJ.setText(editRealName,response.getData().getRealName());
        }
    }


    @Override
    public void onGetDataError(Throwable throwable) {
        tvNextStep.setClickable(true);
        dismissLoadingView();
    }

    private void fillTextView(BindedCardDataLJ data) {
        if (editRealName != null && !StringUtilsLJ.isEmpty(data.getRealName())) {
            editRealName.setText(data.getRealName());
        }
        if (editSelectBank != null && !StringUtilsLJ.isEmpty(data.getBankCode())) {
            editSelectBank.setTag(data.getBankCode());
        }
        if (editBankNumber != null && !StringUtilsLJ.isEmpty(data.getBankCode())) {
            editBankNumber.setText(data.getBankCard());
        }
        if (editSelectBank != null && !StringUtilsLJ.isEmpty(data.getBankValue())) {
            editSelectBank.setText(data.getBankValue());
        }
    }

    @Override
    public void onGetDataNext(ArrayList<ArrayList<DictionaryItemDataLJ>> responseWrapper) {
        if (responseWrapper.size() >= 1) {
            mBankList = responseWrapper.get(0);
        }
    }

    public MenuDialogLJ getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialogLJ(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    private void pickerBank() {
        getMenuDialog().showDialog(editSelectBank, mBankList);
    }
}
